#!/bin/python

# --------------------------------------
# VIDEO BUDDHA - console
# --------------------------------------
# Ranames files based on a specific regex pattern in 
#  show_list.db and based on a specific list of file 
#  extensions.
# --------------------------------------

import argparse
import sys

import models.database
import models.renamer

db = models.database.Database()
rnm = models.renamer.Renamer(1)

print("\nVideo Buddha - Console")
print("---------------------------")

parser = argparse.ArgumentParser(description='Video Buddha console - Mass video file renamer')
parser.add_argument('-d', '--directory', dest='dir', help='Directory to rename files in', nargs=1, action='store')
parser.add_argument('-i', '--insert', dest='insert', help='Insert a definition into the database', nargs=2, action='store', metavar=('"old name"', '"new name"'))
parser.add_argument('-l', '--list_definitions', dest='defs', help='List all of the definitions in the database',  action='store_true')
args = parser.parse_args()

# Is dir set?
if args.dir > 0:
	dir = args.dir[0]
	print dir
	
	# Check dir
	if rnm.path_exists(dir):			
		# Update the status bar with what happened
		#  videos either renamed or, none matched
		rnm.set_dir(dir)
		
		videos = rnm.get_rename_list()
		count = 0
		for (oldFile, rFile) in videos:
			rnm.rename_file(dir, oldFile, rFile)
			count += 1
		
		if count == 0:
			print("Alert: No files were renamed.")
		
		if count > 0:				
			print(str(count) + " file(s) renamed.")	
elif args.defs:
	print("Definitions listing:")
	print("Old Name                  | New name")
	print("-------------------------------------")
	
	defs = db.retrieve()
	
	for (id, oldname, newname) in defs:
		while len(newname) < 25:
			newname = newname + ' '
		while len(oldname) < 25:
			oldname = oldname + ' '
		print(oldname + " | " + newname)
elif args.insert > 0:
	input = args.insert
	print("Old name: " + input[0])
	print("New name: " + input[1])
	
	db.insert(input[0], input[1])
else:
	rnm.get_path()
	videos = rnm.get_rename_list()
	count = 0
	for (oldFile, rFile) in videos:
		rnm.rename_file(dir, oldFile, rFile)
		count += 1
	
	if count == 0:
		print("Alert: No files were renamed.")
	
	if count > 0:				
		print(str(count) + " file(s) renamed.")
	